<?php

namespace behaviorLog;

use think\facade\Env;

class behaviorLog {

    /**
     * @desc 获取所有控制器方法和注释
     * @param string $module
     * @return array
     */
    public static function executeAction($module = 'admin') {
        $i = 0;
        $data = [];
        $all_controller = self::getController($module);
        foreach ($all_controller as $controller) {
            $controller_name = $controller;
            $all_action = self::getAction($module, $controller_name);
            foreach ($all_action as $action) {
                $data[$i] = [
                    'action_name'    => $controller . '_' . $action,
                    'action_comment' => self::getComment($module, $controller, $action),
                ];
                $i++;
            }
        }
        return $data;
    }

    /**
     * @desc 获取所有控制器名称
     * @param string $module 模块
     * @return array|bool
     */
    protected static function getController($module) {
        if(empty($module)) {
            return false;
        }
        $ds = DIRECTORY_SEPARATOR;
        $module_path = Env::get('app_path'). $ds . $module . $ds . 'controller';
        if(!is_dir($module_path)) {
            return false;
        }
        $module_path .= '/*.php';
        $ary_files = glob($module_path);
        $files = [];
        foreach ($ary_files as $file) {
            if (is_dir($file)) {
                continue;
            }else {
                $files[] = basename($file, '.php');
            }
        }
        return $files;
    }

    /**
     * @desc 获取所有方法名称
     * @param string $module 模块
     * @param string $controller 控制器
     * @return array|bool
     */
    protected static function getAction($module = 'admin', $controller) {
        if(empty($controller)) {
            return false;
        }
        $ds = DIRECTORY_SEPARATOR;
        $content = file_get_contents(Env::get('app_path') . $module . $ds . 'controller' . $ds . $controller . '.php');

        preg_match_all("/.*?public.*?function(.*?)\(.*?\)/i", $content, $matches);
        $functions = $matches[1];
        //排除部分方法
        $remove_functions = [
            '_initialize',
            '__construct',
            'getActionName',
            'isAjax',
            'display',
            'show',
            'fetch',
            'buildHtml',
            'assign',
            '__set',
            'get',
            '__get',
            '__isset',
            '__call',
            'error',
            'success',
            'ajaxReturn',
            'redirect',
            '__destruct',
            '_empty'
        ];
        $customer_functions = [];
        foreach ($functions as $func) {
            $func = trim($func);
            if(!in_array($func, $remove_functions)) {
                if (strlen($func)>0) $customer_functions[] = $func;
            }
        }
        return $customer_functions;
    }

    /**
     * @desc 获取方法注释
     * @param string $module 模块
     * @param string $controller 控制器
     * @param string $action 方法
     * @return string|bool
     * @throws \Exception
     */
    protected static function getComment($module = 'admin', $controller = '', $action = '') {
        $desc= '\\app\\'. $module .'\\controller\\' . $controller;

        //php反射机制
        $func  = new \ReflectionMethod(new $desc(), $action);
        //获取当前function注释
        $comment   = $func->getDocComment();
        //匹配@name注释
        $flag = preg_match_all('/@desc(.*?)\n/',$comment,$comment);
        if(!$flag){
            return '';
        }
        $comment = trim($comment[1][0]);
        $comment = $comment ? $comment : '';
        return $comment;
    }
  
}
